//
// Ishtar C++ Utility Library
//
// Task Facility - Worker Thread Private Header
//

#ifndef __ISHTAR_TASK_WORKER_THREAD_IMPL_H
#define __ISHTAR_TASK_WORKER_THREAD_IMPL_H

#include <Ishtar/Ishtar.h>

#if defined( ISHTAR_COMPILER_HAS_PRAGMA_ONCE )
#pragma once
#endif

#include <Ishtar/Concurrent/ConcurrentBlockingQueue.h>
#include <Ishtar/Task/WorkerThread.h>
#include <Ishtar/Task/WorkExecutorImpl.h>
#include <Ishtar/Thread/ThreadExecution.h>
#include <Ishtar/Thread/SpinMutex.h>


namespace Ishtar
{

namespace Task
{

///////////////////////////////////////////////////////////////////////////////
//
// Worker Thread
//

class WorkerThreadImpl : public WorkExecutorImpl
                       , public ThreadExecution
{
public:

    WorkerThreadImpl();

    void Startup( const std::string& name );
    void Shutdown();

    void Submit( const WorkImplPtr& work );

    Uint GetNumReadyWorks() const { return m_readyWorks.UnsafeSize(); }


private:

    //
    // implements ThreadExecution
    //

    std::string GetName() const { return m_name; }

    Int Execute();  // thread main entry


    //
    // implements WorkExecutorImpl
    //

    void AddReadyWork( const WorkImplPtr& w );


    //
    // Data Members
    //

    std::string m_name;

    SpinMutex m_addReadyMutex;

    Bool m_shutdown;

    typedef ConcurrentBlockingQueue< WorkImplPtr > WorkQueue;
    WorkQueue m_readyWorks;

};


///////////////////////////////////////////////////////////////////////////////

} // namespace Task

} // namespace Ishtar

#endif // __ISHTAR_TASK_WORKER_THREAD_IMPL_H
